package com.ddx.service.impl;

import com.ddx.common.exception.ResultModel;
import com.ddx.dao.ShoppingStoreMapper;
import com.ddx.dto.StoreDto;
import com.ddx.pojo.ShoppingStore;
import com.ddx.service.IShoppingProductService;
import com.ddx.service.StoreService;
import com.google.common.collect.ImmutableMap;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.Map;

/**
 * @author: liyuxiang
 * @create: 2019-01-06
 */
@Service
public class StoreServiceImpl implements StoreService {
    private static final Logger logger = LoggerFactory.getLogger(StoreServiceImpl.class);

    @Autowired
    ShoppingStoreMapper storeMapper;

    @Autowired
    IShoppingProductService productService;
    @Value("${fastdfs.server}")
    String fastdfsServer;

    @Override
    public ResultModel register(int userId, StoreDto storeDto) {
        ShoppingStore shoppingStore = new ShoppingStore();
        shoppingStore.setName(storeDto.getName());
        shoppingStore.setIntroduce(storeDto.getIntroduce());
        shoppingStore.setPicture(storeDto.getPicture());
        shoppingStore.setUserId(String.valueOf(userId));
        int storeId = storeMapper.insertSelective(shoppingStore);
        Map<String, Object> map = ImmutableMap.of("storeId", storeId);
        ResultModel resultModel = ResultModel.getSuccessInstance();
        resultModel.setData(map);
        return resultModel;
    }

    @Override
    public ShoppingStore index(int userId) {
        ShoppingStore shoppingStore = storeMapper.selectByUserId(userId);
//        Map<String, Object> productMap = productService.getProductMap(shoppingStore.getId(), index, size, 0);
        shoppingStore.setPicture(fastdfsServer+shoppingStore.getPicture());

        return shoppingStore;
    }

    @Override
    public ResultModel edit(int userId, StoreDto storeDto) {
        logger.info("修改店铺入参userId={}，storeDto={}",userId,storeDto);
        ShoppingStore shoppingStore = new ShoppingStore();
        shoppingStore.setName(storeDto.getName());
        shoppingStore.setIntroduce(storeDto.getIntroduce());
//        shoppingStore.setPicture(storeDto.getPicture());
        shoppingStore.setUserId(String.valueOf(userId));
        int storeId = storeMapper.updateByUserId(shoppingStore);
        Map<String, Object> map = ImmutableMap.of("storeId", storeId);
        ResultModel resultModel = ResultModel.getSuccessInstance();
        resultModel.setData(map);
        return resultModel;
    }
}
